package com.atguigu.controller;


import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.atguigu.config.AlipayConfig;
import com.atguigu.entity.OrderInfo;
import com.atguigu.entity.PaymentInfo;
import com.atguigu.enums.PaymentStatus;
import com.atguigu.result.RetVal;
import com.atguigu.service.PaymentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/payment")
public class PaymentController {
    @Autowired
    PaymentInfoService paymentInfoService;
    @RequestMapping("createQrCode/{orderId}")
    public String createQrCode(@PathVariable Long orderId) throws AlipayApiException {
        String as=paymentInfoService.createQrCode(orderId);
        return as;
    }

    @PostMapping("/async/notify")
    public String notify(@RequestParam Map<String,String> aliParamMap) throws AlipayApiException {
        boolean signVerified = AlipaySignature.rsaCheckV1(aliParamMap, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type);
        if (signVerified){
            //修改支付订单信息
            //根据商户订单号查询支付订单信息
            String tradeStatus = aliParamMap.get("trade_status");
            if ("TRADE_SUCCESS".equals(tradeStatus)||"TRADE_FINISHED".equals(tradeStatus)){
                String outTradeNo = aliParamMap.get("out_trade_no");
                PaymentInfo paymentInfo = paymentInfoService.getPaymentInfo(outTradeNo);
                //如果我的本地支付订单信息是已经关闭和已经支付 支付宝就不需要再次调用
                if (paymentInfo.getPaymentStatus().equals(PaymentStatus.PAID.name())||paymentInfo.getPaymentStatus().equals(PaymentStatus.ClOSED.name())){
                    return "failure";
                }
                //修改订单信息
                paymentInfoService.updatePaymentInfo(aliParamMap);
            }
        }else {
// TODO 验签失败则记录异常日志，并在response中返回failure.
            return "failure";
        }
        return "failure";
    }

    //3.发起退款 http://127.0.0.1:8005/payment/refund/28
    @RequestMapping("/refund/{orderId}")
    public RetVal refund(@PathVariable Long orderId) throws Exception{
        boolean flag = paymentInfoService.refund(orderId);
        return RetVal.ok(flag);
    }

    //查询支付宝是否有交易记录
    //4.查询支付宝中是否有交易记录
    @RequestMapping("queryAlipayTrade/{orderId}")
    public boolean queryAlipayTrade(@PathVariable Long orderId) throws Exception {
        return paymentInfoService.queryAlipayTrade(orderId);
    }

    //5.关闭交易
    @RequestMapping("closeAlipayTrade/{orderId}")
    public RetVal closeAlipayTrade(@PathVariable Long orderId) throws Exception {
        boolean flag=paymentInfoService.closeAlipayTrade(orderId);
        return RetVal.ok(flag);
    }

    //6.查询支付订单信息
    @GetMapping("getPaymentInfo/{outTradeNo}")
    public PaymentInfo getPaymentInfo(@PathVariable String outTradeNo) {
        return paymentInfoService.getPaymentInfo(outTradeNo);
    }

}
